package com.sc.nft.hna.form;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.sc.nft.config.hna.HnaConfig;
import com.sc.nft.dto.HnaRefundDto;
import com.sc.nft.exception.GlobalRunTimeException;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;

/**
 * @Author @Version V1.0 @Description @Date 2020-10-19
 */
@Data
public class T005TransForm extends BaseTransForm {

  private static final long serialVersionUID = 1L;

  public static final String[] encryptArr =
      new String[] {
        "serialId",
        "orgMerOrderId",
        "orgSubmitTime",
        "orderAmount",
        "refundSource",
        "destType",
        "refundType",
        "refundAmount",
        "notifyUrl",
        "remark"
      };
  public static final String[] verifyArr =
      new String[] {
        "version",
        "tranCode",
        "merOrderId",
        "merId",
        "charset",
        "signType",
        "resultCode",
        "errorCode",
        "orgMerOrderId",
        "orderAmount",
        "refundAmount"
      };
  public static final String[] notifyVerifyArr = verifyArr;
  public static final String[] submitArr =
      new String[] {
        "version",
        "tranCode",
        "merId",
        "merOrderId",
        "submitTime",
        "msgCiphertext",
        "signType",
        "signValue",
        "merAttach",
        "charset"
      };

  private String serialId; // 请求序列号
  private String orgMerOrderId; // 商户原始订单号
  private String orgSubmitTime; // 原订单支付下单请求时间
  private String orderAmount; // 原订单金额
  private String refundSource; // 退款资金来源
  private String destType; // 退款目的地类型
  private String refundType; // 退款类型
  private String refundAmount; // 商户退款金额
  private String notifyUrl; // 异步通知地址
  private String remark; // 备注

  public static final String MAIN_URL = "https://ncount.hnapay.com/api/t005.htm";
  public static final String TRAN_CODE = "T005";

  @Override
  public String getEncryptJsonStr() {
    return getJsonStr(this, encryptArr);
  }

  @Override
  public String getVerifyJsonStr() {
    return getJsonStr(this, verifyArr);
  }

  @Override
  public String getSubmitJsonStr() {
    return getJsonStr(this, submitArr);
  }

  public static Map<String, Object> generateParam(HnaConfig hnaConfig, HnaRefundDto dto) {
    T005TransForm transForm = new T005TransForm();
    BeanUtils.copyProperties(dto, transForm);
    transForm.setSerialId("metago" + DateUtil.format(new Date(), DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
    transForm.setRefundSource("1");
    transForm.setDestType("1");
    transForm.setRefundType(dto.getRefundType());
    if (StringUtils.isBlank(dto.getNotifyUrl())) {
      transForm.setNotifyUrl(hnaConfig.getT005NotifyUrl());
    } else {
      transForm.setNotifyUrl(dto.getNotifyUrl());
    }
    transForm.initCommonParams(TRAN_CODE, hnaConfig.getMerId(), IdWorker.getIdStr(), "");
    return generateParam(transForm, hnaConfig);
  }
}
